import time
import os.path
from activist.Config import Config

if not os.path.isdir(os.path.join(Config.base,'data/commands.log')):
    os.system( 'mkdir %s'%os.path.join(Config.base,'data/commands.log') )

from activist.DataStore import DataStore
from activist.Person import Person
from activist.Group import Group
from activist.Place import Place
from activist.Event import Event
from activist.Comment import Comment
from activist.GenericData import GenericData
from activist.EventInformation import EventInformation
from activist.MetaData import MetaData
from activist.CommandProcessor import CommandProcessor
from activist.command import GenericCommand
import activist.Place
import activist.Person
import activist.Event
import activist.Time


highlight = GenericData('highlighted events')
moderated = GenericData('moderated')

personA = Person('mike warren', '123-4567', 'foo@bar.com', None)
personB = Person('some one', '123-5555', 'nothing@nowhere.com', '#4 1492 64th Street NW\nCalgary, Alberta')



try:
    personA.info()._name = 'foo'
except:
    pass

placeA = Place( 'Some Place', '456 Foo Bar Avenue', ['555-1234','555-4321'] )
placeB = Place( 'A Location', '#1 1492 64th Street NW\nCalgary, Alberta', ['555-1234','555-4321'] )

eventA = Event( personA, 'Random Event', activist.Time.now(), 'Some random event, happening right now!', placeA )
eventA._test()
eventB = Event( personB, 'Another Event', activist.Time.now(), 'Another event...', placeB )
eventC = Event( personB, 'Much Early Event', activist.Time.Moment(time.time()-86400.0), 'Event description (should be from yesterday)',placeB)
eventD = Event( personB, 'Slightly Early Event', activist.Time.Moment(time.time()-7200.0), 'Event description (should be from two hours ago)',placeB)

eventA.approve(personA)
eventB.approve(personA)
eventC.approve(personB)

eventE = Event( personA, 'Upcoming Event', activist.Time.Moment(time.time()+86400.0*2+3600.0), 'Event description (should be two days from now plus an hour).',placeB)

eventF = Event( personA, 'Soonly Upcoming Event', activist.Time.Moment(time.time()+2.5*3600.0), 'Event description (should be two and a half hours from now).',placeB)

MetaData(eventA,MetaData.BelongsWith,highlight)

personB = Person('foo bar', '765-4321', 'bar@foo.com', '123 Fake Street')

groupA = Group( 'Fooity Bar', 'A random group of non-people.',
                [personA,personB] )

MetaData(eventA,MetaData.BelongsWith,groupA)

if False:
    for x in range(0,50):
        e = Event( personB, 'Filler Event ' + str(x), activist.Time.now(), 'A filler event.', placeB )

####


cmd = GenericCommand( activist.Place.commandCreate,
                      ("test place", "test place's address", ["(403) 555-1234"]) )
place = CommandProcessor.process(cmd)

cmd = GenericCommand( activist.Person.commandCreate,
                      ( "test person", ["(403) 555-4321"], ["test@person.com"], "test person's address" ) )
person = CommandProcessor.process(cmd)

when = activist.Time.now()
cmd = GenericCommand(activist.Event.commandCreate,
                     ( person, 'Some Test Event', when, r"""
Come and create testing events for the new activist nework code. Mike
will be on hand to provide motivation and discipline.

If you think this might be fun, bring a friend, some patience and
your computer.

testing rST:

This is a paragraph.  It's quite
short.

   This paragraph will result in an indented block of
   text, typically used for quoting other text.

This is another one. I have some *highlightted* and **strongly
highlighted** text in here, too. What fun! What does a URL look like? 
http://www.mike-warren.com

How about if I want `a link with a name`_ to the same place?

.. _a link with a name: http://www.mike-warren.com

* a bullet point using "*"

  - a sub-list using "-"

      + yet another sub-list

        - another item

Let's try an example::

   this is an example with some * of the *weirdo*
   formatting \and \stuff{} which rST uses.

        whoa!

example over.

I Am A Title
------------

I'm a paragraph below the above title.

                     """, place ) )
event = CommandProcessor.process(cmd)



####

person = activist.Person.Person('foo foo', '123-4567', 'foo@bar.com', None)
info = person.info()
cmd = GenericCommand(activist.Person.commandChange,
                     ( None,
                       person,
                       'mike warren',
                       ['(403) 123-4567'],
                       ['foo@bar.com','mike@somewhere.com'],
                       'an address' ) )
import time                     # so that the metadata creation-time has a chance to be different
time.sleep(0.1)
CommandProcessor.process( cmd )

                                # test editing Event

place0 = activist.Place.Place('some place', 'somewhere', '(403) 282-1234')
place1 = activist.Place.Place('different place', 'somewhere', '(403) 282-4321')

event = activist.Event.Event(person,
                             'Some Placed Event', activist.Time.now(), "an event", place0 )
cmd = GenericCommand(activist.Event.commandChange,
                     (person,
                      event,
                      event.info().title(), activist.Time.now(), "an *exciting* event", place1))
import time
time.sleep(0.1)
CommandProcessor.process(cmd)



###
### some comments
###

commentA = Comment( eventA, 'This event was fun.', 'I had a lot of fun at this event.', personA )
commentB = Comment( eventA, 'This event was weird.', 'I didn\'t know what to expect at this event, but I felt weird anyway.', personA )
commentC = Comment( commentA, 'You\'re wrong.', 'I disagree with you.', personB )
commentD = Comment( commentC, 'Well, this could just get silly...', 'I could keep replying, theoretically, forever.', personB )
commentE = Comment( commentD, 'Silly, silly, silly...', 'I could keep replying, theoretically, forever.', personB )


## leave me last
DataStore.save()


keys = DataStore.keys()
def foo(a,b):
    return cmp(int(a),int(b))
keys.sort(foo)
for key in keys:
    print key,DataStore[key]
